package com.matrix.draft.jvm.jmm;

import lombok.SneakyThrows;

/**
 * JMM原子性测试
 */
public class ManipulateStatic {

    public static int a = 0;

    public static Object lock  = new Object();

    @SneakyThrows
    public static void main(String[] args) {
        for (int j = 0; j < 10; j++) {
            Thread t1 = new Thread(() -> {
                for (int i = 0; i < 5000; i++) {
                    synchronized (lock){
                        ++ManipulateStatic.a;
                    }
                }
            });
            Thread t2 = new Thread(() -> {
                for (int i = 0; i < 5000; i++) {
                    synchronized (lock){
                        --ManipulateStatic.a;
                    }
                }
            });
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            System.out.println(ManipulateStatic.a);
        }


    }
}
